package com.duanjw.bank.rate.handler;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.duanjw.bank.rate.core.BankRate;
import com.duanjw.bank.rate.core.BankRateHandler;
import com.duanjw.bank.rate.core.DateFormatUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

/**
 * 交通银行
 *
 * @author duanjw
 * @date 2024/4/1
 */
public class JiaoTongBanRateHandler implements BankRateHandler {
    @Override
    public BankRate getBankRate() throws IOException {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"); // 仿真
        Connection connect = Jsoup.connect("https://www.bankcomm.com/SITE/getRMBDepositRateDate.do");
        String responseBody = connect.headers(headers).ignoreContentType(true).method(Connection.Method.POST).execute().body();

        JSONObject rspBody = new JSONObject(responseBody).getJSONObject("RSP_BODY");
        JSONObject responseJson = rspBody.getJSONObject("allRates");
        JSONArray responseDateList = rspBody.getJSONArray("dateList");

        String kaiShiZhiXingRiQi = new JSONObject(responseDateList.get(0)).getStr("sdt");

        String huoQiCunKuanLiLv = getLiLv(responseJson, "15601000", kaiShiZhiXingRiQi);

        String zhengCunZhengQu3YueLiLv = getLiLv(responseJson, "15601101", kaiShiZhiXingRiQi);
        String zhengCunZhengQu6YueLiLv = getLiLv(responseJson, "15601102", kaiShiZhiXingRiQi);
        String zhengCunZhengQu12YueLiLv = getLiLv(responseJson, "15601103", kaiShiZhiXingRiQi);
        String zhengCunZhengQu24YueLiLv = getLiLv(responseJson, "15601104", kaiShiZhiXingRiQi);
        String zhengCunZhengQu36YueLiLv = getLiLv(responseJson, "15601105", kaiShiZhiXingRiQi);
        String zhengCunZhengQu60YueLiLv = getLiLv(responseJson, "15601106", kaiShiZhiXingRiQi);

        String lingCunZhengQu12YueLiLv = getLiLv(responseJson, "15601200", kaiShiZhiXingRiQi);
        String lingCunZhengQu36YueLiLv = getLiLv(responseJson, "15601201", kaiShiZhiXingRiQi);
        String lingCunZhengQu60YueLiLv = getLiLv(responseJson, "15601202", kaiShiZhiXingRiQi);

        String dingHuoLiangBian3YueLiLv = new BigDecimal(zhengCunZhengQu3YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        String dingHuoLiangBian6YueLiLv = new BigDecimal(zhengCunZhengQu6YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        String dingHuoLiangBian12YueLiLv = new BigDecimal(zhengCunZhengQu12YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();

        String tongZhiCunKuan1TianLiLv = getLiLv(responseJson, "15602700", kaiShiZhiXingRiQi);
        String tongZhiCunKuan7TianLiLv = getLiLv(responseJson, "15602701", kaiShiZhiXingRiQi);

        String xieDingCunKuanLiLv = getLiLv(responseJson, "15601900", kaiShiZhiXingRiQi);

        return new BankRate(DateFormatUtil.format3(kaiShiZhiXingRiQi), huoQiCunKuanLiLv, zhengCunZhengQu3YueLiLv, zhengCunZhengQu6YueLiLv, zhengCunZhengQu12YueLiLv, zhengCunZhengQu24YueLiLv, zhengCunZhengQu36YueLiLv, zhengCunZhengQu60YueLiLv, lingCunZhengQu12YueLiLv, lingCunZhengQu36YueLiLv, lingCunZhengQu60YueLiLv, dingHuoLiangBian3YueLiLv, dingHuoLiangBian6YueLiLv, dingHuoLiangBian12YueLiLv, tongZhiCunKuan1TianLiLv, tongZhiCunKuan7TianLiLv, xieDingCunKuanLiLv);
    }

    @Override
    public String getBankName() {
        return "交通银行";
    }

    private String getLiLv(JSONObject responseJson, String key, String riQi) {
        String liLv = responseJson.getJSONObject(key).getStr(riQi);
        if (Objects.isNull(liLv)) {
            Iterator<Map.Entry<String, Object>> iterator = responseJson.getJSONObject(key).iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> next = iterator.next();
                LocalDate date1 = LocalDate.parse(next.getKey()); // 返回值中的日期，2023-12-11
                LocalDate date2 = LocalDate.parse(riQi); // 要查询的日期，2023-01-01
                if(date1.isBefore(date2)) { // date1 在 date2 之前
                    liLv = next.getValue().toString();
                }
            }
        }
        return liLv;
    }
}
